#include "button.h"
#include "camerapage.h"
#include "aboutwidget.h"
#include <QDebug>
#include <QPainter>
#include "mainwindow.h"

QGSettings *Button::m_pGsettingTheme; //lhl debug
bool Button::video_start = false;
bool Button::video_mode = false;
bool Button::voice_mode = false;
bool Button::picturepage_show = false;
bool Button::delay_mode = false;
Button::Button(QWidget *parent)
  : QWidget(parent)
{
    m_pGsettingTheme = new QGSettings(FITTHEMEWINDOW);  //安装窗口 lhl debug
    switchLayout = new QHBoxLayout;
    btnLayout = new QHBoxLayout;
    vLayout = new QVBoxLayout;

    capture = new QPushButton ;
    video = new QPushButton;
    cheese = new QPushButton;
    cheese_dark = new QPushButton;
    voice = new QPushButton;
    picture = new QLabel;
    stop = new QPushButton;
    cheese_video = new QPushButton;
    cheese_stop = new QPushButton;
    restore = new QPushButton;
    delay = new QPushButton;
    grid = new QPushButton;
    layout = new QPushButton;
    mirror = new QPushButton;
    cheese_dark->hide();  //lhl debug

    btnLayout->addSpacing(20);
    btnLayout->addWidget(picture);
    btnLayout->addStretch(99);
    btnLayout->addWidget(delay);
    btnLayout->addWidget(cheese);
    btnLayout->addWidget(cheese_dark);
    btnLayout->addWidget(cheese_video);
    btnLayout->addWidget(cheese_stop);
//    btnLayout->addWidget(grid);
    btnLayout->addWidget(mirror);
//    btnLayout->addWidget(restore);
    btnLayout->addStretch(99);
    btnLayout->addSpacing(70);
    btnLayout->setMargin(0);


    switchLayout->addStretch(99);
    switchLayout->addWidget(capture);
    switchLayout->addSpacing(-8);
    switchLayout->addWidget(video);
    switchLayout->addStretch(99);
    switchLayout->setMargin(0);

    vLayout->addLayout(switchLayout);
    vLayout->addLayout(btnLayout);
    vLayout->setMargin(0);
    this->setLayout(vLayout);


    cheese_video->hide();
    cheese_stop->hide();
    restore->hide();
    stop->hide();
    voice->hide();

    cheese_stop->setFixedSize(50,50);
    cheese_video->setFixedSize(50,50);
    cheese->setFixedSize(50,50);
    cheese_dark->setFixedSize(50,50);
    capture->setFixedSize(48,24);
    video->setFixedSize(48,24);
    picture->setFixedSize(48,48);
    delay->setFixedSize(48,32);

    mirror->setFixedSize(48,32);

//    grid->setFixedSize(48,32);
    capture->setCheckable(true);
    video->setCheckable(true);
    delay->setCheckable(true);
//    grid->setCheckable(true);
    cheese->setCheckable(true);
    cheese_dark->setCheckable(true);
    mirror->setCheckable(true);
//    picture->setCheckable(true);

    //从配置文件中读取之前的配置
    if(set->value("model").toInt()){
        cheese->show();
        cheese_dark->hide();
        cheese_video->hide();
        cheese_stop->hide();
        voice->hide();
        stop->hide();
//        grid->show();
        delay->show();
        video_mode = false;
        capture->setChecked(1);
        video->setChecked(0);
#ifdef __V10__
        capture->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
#endif
    }
    else{
        video_mode = true;
        capture->setChecked(0);
        video->setChecked(1);
        delay->hide();
        layout->hide();
        mirror->hide();
//        grid->hide();
#ifdef __V10__
      video->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
#endif
        cheese->hide();
        cheese_video->show();
    }

    QString btnStyle="QPushButton{border-radius:4px;}"
                     "QPushButton:hover{;}"
                     "QPushButton:pressed{;}";
    cheese->setStyleSheet(btnStyle);
    cheese_dark->setStyleSheet(btnStyle);
    cheese_stop->setStyleSheet(btnStyle);
    cheese_video->setStyleSheet(btnStyle);

#ifdef __V10__
    capture->setIcon(QIcon(":/image/camera1.png"));
    video->setIcon(QIcon(":/image/video1.png"));
    cheese->setIcon(QIcon(":/image/paizhao-kaishi.png"));
    cheese_dark->setIcon(QIcon(":/image/shot-dark.png"));
    voice->setIcon(QIcon(":/image/maike-on .svg"));
    picture->setIcon(QIcon(":/image/picture1.png"));
    cheese_video->setIcon(QIcon(":/image/luxiang-kaishi.png"));
    cheese_stop->setIcon(QIcon(":/image/luxiang-zanting.png"));
    stop->setIcon(QIcon(":/image/zanting1.png"));
    restore->setIcon(QIcon(":/image/media-playback-start-symbolic.png"));
#else
    capture->setIcon(QIcon(":/image/camera.svg"));
    video->setIcon(QIcon(":/image/film.svg"));
    voice->setIcon(QIcon("audio-input-microphone-symbolic"));
//    picture->setIcon(QIcon::fromTheme("view-list-images-symbolic"));
    cheese->setIcon(QIcon(":/image/shot-click.svg"));
    cheese_dark->setIcon(QIcon(":/image/shot-dark.png"));
    cheese_video->setIcon(QIcon(":/image/actiondark-click.svg"));
    cheese_stop->setIcon(QIcon(":/image/complete.svg"));
    stop->setIcon(QIcon("media-playnack-pause-symbolic"));
    restore->setIcon(QIcon("video-x-generic-symbolic"));
    delay->setIcon(QIcon(":/image/delayed.svg"));
//    grid->setIcon(QIcon(":/image/grid.svg"));
    layout->setIcon(QIcon(":/image/layout.svg"));
    mirror->setIcon(QIcon(":/image/mirroring.svg"));

    capture->setToolTip(tr("capture mode"));
    video->setToolTip(tr("video mode"));
    cheese->setToolTip(tr("cheese"));
    cheese_dark->setToolTip(tr("cheese"));
    cheese_video->setToolTip(tr("video"));
    cheese_stop->setToolTip(tr("stop"));
    picture->setToolTip(tr("album"));
    delay->setToolTip(tr("delay"));
    mirror->setToolTip(tr("mirror"));

    //适配窗管
    picture->setProperty("useIconHighlightEffect", 0x8);
    capture->setProperty("useIconHighlightEffect", 0x8);
    video->setProperty("useIconHighlightEffect", 0x8);
    cheese->setProperty("useIconHighlightEffect", 0x8);
    cheese_dark->setProperty("useIconHighlightEffect", 0x8);
    delay->setProperty("useIconHighlightEffect", 0x8);
//    grid->setProperty("useIconHighlightEffect", 0x8);
    mirror->setProperty("useIconHighlightEffect", 0x8);
#endif

    cheese->setIconSize(QSize(50,50));
    cheese_dark->setIconSize(QSize(50,50));
    capture->setIconSize(QSize(22,20));
    video->setIconSize(QSize(22,20));
    voice->setIconSize(QSize(22,20));
    stop->setIconSize(QSize(22,20));
    restore->setIconSize(QSize(22,20));
    mirror->setIconSize(QSize(22,20));
    cheese_video->setIconSize(QSize(50,50));
    cheese_stop->setIconSize(QSize(50,50));

//    capture->setFlat(true);
//    video->setFlat(true);
//    voice->setFlat(true);
//    cheese->setFlat(true);
//    stop->setFlat(true);
//    cheese_video->setFlat(true);
//    cheese_stop->setFlat(true);
//    delay->setFlat(true);
//    grid->setFlat(true);
//    picture->setFlat(true);

    delay->setFocusPolicy(Qt::NoFocus);
//    grid->setFocusPolicy(Qt::NoFocus);
    capture->setFocusPolicy(Qt::NoFocus);
    video->setFocusPolicy(Qt::NoFocus);
    voice->setFocusPolicy(Qt::NoFocus);
    cheese->setFocusPolicy(Qt::NoFocus);
    cheese_dark->setFocusPolicy(Qt::NoFocus);
    stop->setFocusPolicy(Qt::NoFocus);
    cheese_video->setFocusPolicy(Qt::NoFocus);
    cheese_stop->setFocusPolicy(Qt::NoFocus);
    picture->setFocusPolicy(Qt::NoFocus);
    mirror->setFocusPolicy(Qt::NoFocus);

    connect(capture,&QPushButton::clicked,this,&Button::capture_click);
    connect(video,&QPushButton::clicked,this,&Button::video_click);
    connect(cheese,&QPushButton::clicked,this,&Button::cheese_click);
    connect(cheese_dark,&QPushButton::clicked,this,&Button::cheese_dark_click);
    //connect(cheese_video,&QPushButton::clicked,this,&Button::cheese_video_click);
    connect(cheese_stop,&QPushButton::clicked,this,&Button::cheese_stop_click);
    connect(voice,&QPushButton::clicked,this,&Button::voice_click);
    connect(stop,&QPushButton::clicked,this,&Button::stop_click);
//    connect(picture,&QPushButton::clicked,this,&Button::picture_click);
    connect(restore,&QPushButton::clicked,this,&Button::restore_click);
//    connect(Burst,&QPushButton::clicked,this,&Button::Burst_click);
    connect(m_pGsettingTheme,&QGSettings::changed,this,&Button::dealSystemGsettingChange); //lhl debug

    //拍照按钮的图标变换
    //获取主题 lhl debug
    nowThemeStyle = m_pGsettingTheme->get("styleName").toString();
    qDebug() << "has_device:" << CameraPage::has_device;
    //拍照按钮的图标变换 黑夜模式
    if(!video_mode){
        if(nowThemeStyle == "ukui-dark" || nowThemeStyle == "ukui-black"){
            cheese_dark->show();
            cheese->hide();
        }
        else{
            cheese_dark->hide();
            cheese->show();
        }
    }

    //拍照按钮的图标变换
    connect(cheese_dark,&QPushButton::pressed,this,[=](){
        if(!video_mode && CameraPage::has_device){
            cheese_dark->setIcon(QIcon(":/image/shot-dark-click.svg"));
        }
    });
    connect(cheese,&QPushButton::pressed,this,[=](){
        if(!video_mode && CameraPage::has_device){
            cheese->setIcon(QIcon(":/image/shot-hover.svg"));
        }
    });

    this->setAttribute(Qt::WA_StyledBackground);
//    this->setStyleSheet("QWidget{background-color:#000000;}");
}

Button::~Button(){

}

void Button::paintEvent(QPaintEvent* event){
    QPainter painter(this);
    if(!AboutWidget::m_pGsettingThemeData->isSchemaInstalled(FITTHEMEWINDOW))
       AboutWidget::m_pGsettingThemeData = new QGSettings(FITTHEMEWINDOW);
    QString themeNow = AboutWidget::m_pGsettingThemeData->get("styleName").toString();
    if(video_mode){
        painter.fillRect(rect(),QColor(2,2,2,0.3 * 255));
        return;
    }

    if("ukui-dark" == themeNow || "ukui-black" == themeNow){
        painter.fillRect(rect(),QColor(2,2,2,255));
    }
    else
        painter.fillRect(rect(),QColor(255,255,255,255));

    QWidget::paintEvent(event);

}

void Button::focusInEvent(){
   // QPushButton *pButton = qobject_cast<QPushButton *>(sender());
}


void Button::capture_click(){
    //开始录像时不能切换
    nowThemeStyle = m_pGsettingTheme->get("styleName").toString();
    if(!video_start){  
        if(nowThemeStyle == "ukui-dark" || nowThemeStyle == "ukui-black"){
            cheese_dark->show();
            cheese->hide();
        }
        else{
            cheese_dark->hide();
            cheese->show();
        }
        cheese_video->hide();
        cheese_stop->hide();
        delay->show();
//        grid->show();
        mirror->show();
//        voice->hide();
//        stop->hide();
        video_mode = false;
        capture->setChecked(1);
        video->setChecked(0);
#ifdef __V10__
        capture->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
        video->setStyleSheet("QPushButon{background-color:#000000}");
#endif
        set->setValue("model",1);
    }else{
        capture->setChecked(0);
//        video->setChecked(0);
    }

}

void Button::video_click(){
    if(!video_start && !delay_mode){
        video_mode = true;
//        voice->show();
//        stop->show();
        capture->setChecked(0);
        video->setChecked(1);
#ifdef __V10__
        video->setStyleSheet("QPushButton{background-color:#3D6BE5;border-radius:4px;}");
        capture->setStyleSheet("QPushButon{background-color:#000000}");
#endif
        delay->hide();
        mirror->hide();
        layout->hide();
        cheese->hide();
        cheese_dark->hide();
//        grid->hide();
        cheese_video->show();
        set->setValue("model",0);
//        this->setStyleSheet("{background-color:#020202}");
    }
    if(video_start){
        video->setChecked(1);
    }
    if(delay_mode){
        video->setChecked(0);
    }
}

void Button::Burst_click(){

}
void Button::cheese_click(){
    //拍照模式
    if(!video_mode && CameraPage::has_device){
         cheese->setIcon(QIcon(":/image/shot-click.svg"));
      }
}

void Button::cheese_dark_click(){
    //拍照模式
    if(!video_mode && CameraPage::has_device){
         cheese_dark->setIcon(QIcon(":/image/shot-dark.png"));
      }
}

void Button::cheese_video_click(){
    //录像开始
    if(CameraPage::has_device){
             video_start = 1;
             cheese_stop->show();
             cheese_video->hide();
    }

}

void Button::cheese_stop_click(){

        video_start = 0;
        cheese_stop->hide();
        cheese_video->show();
        if(!restore->isHidden()){
    //        restore->hide();
    //        stop->show();

    }
}

void Button::voice_click(){
    //关闭录音设备
    if(!video_start){
        if(voice_mode){
              voice->setIcon(QIcon(":/image/maike-off .svg"));
              voice_mode = false;
          }
        //打开录音设备
        else{
            voice->setIcon(QIcon(":/image/maike-on .svg"));
            voice_mode = true;
        }
    }
}
void Button::stop_click(){
    if(CameraPage::has_device && (video_start)){
//        restore->show();
//        stop->hide();
    }
}
void Button::restore_click(){
//    stop->show();
//    restore->hide();
}

void Button::picture_click(){
    if(!picturepage_show){
        picturepage_show = true;
//        picture->setChecked(true);
#ifdef __V10__
        picture->setStyleSheet("QPushButton{background-color:#3DBE5;border-radius:4px;}");
#endif
    }
    else{
//        picture->setChecked(false);
        picturepage_show = false;
#ifdef __V10__
        picture->setStyleSheet("QPushButton{background-color:#000000;}");
#endif
    }
}

//冗余代码 需优化
void Button::dealSystemGsettingChange(const QString key){
    if(key == "styleName"){
        refreshThemeBySystemConf();
    }
}
void Button::refreshThemeBySystemConf(){
    QString themeNow = m_pGsettingTheme->get("styleName").toString();
    //拍照按钮的图标变换 黑夜模式
    if(!video_mode){
        if(themeNow == "ukui-dark" || themeNow == "ukui-black"){
            cheese_dark->show();
            cheese->hide();
        }
        else{
            cheese->show();
            cheese_dark->hide();
        }
    }
}

//lhl debug end
void Button::scale_click(){

}

